package com.tian.service;

import com.tian.dto.LoanPlainDto;
import com.tian.entity.LoanPlain;
import com.tian.enums.ResultCode;
import com.tian.mapper.LoanPlainMapper;
import com.tian.utils.CommonResult;
import com.tian.utils.LoanPlainDataConvertUtil;
import org.apache.dubbo.config.annotation.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author tianwc  公众号：java后端技术全栈、面试专栏
 * @version 1.0.0
 * @date 2023年08月28日 15:28
 * 在线刷题 1200+题和1000+篇干货文章：<a href="https://woaijava.cc/">博客地址</a>
 */
@Service(version = "1.0.0")
public class LoanPlainDubboServiceImpl implements LoanPlainDubboService {

    @Resource
    private LoanPlainMapper loanPlainMapper;

    @Override
    public CommonResult<LoanPlainDto> queryById(Long id) {
        LoanPlain loanPlain = loanPlainMapper.selectByPrimaryKey(id);
        if (loanPlain == null) {
            return CommonResult.failed(ResultCode.PARAMETER_ERROR);
        }
        //set("",)
        LoanPlainDto loanPlainDto = LoanPlainDataConvertUtil.convert(loanPlain);
        return CommonResult.success(loanPlainDto);
    }

    @Override
    public CommonResult<List<LoanPlainDto>> querList() {
        //查到还款时间是明天还款计划
        List<LoanPlain> loanPlainList = loanPlainMapper.selectAll();
        //数据转换
        List<LoanPlainDto> loanPlainDtoList = convert(loanPlainList);
        //组装返回参数
        return CommonResult.success(loanPlainDtoList);
    }

    private List<LoanPlainDto> convert(List<LoanPlain> loanPlainList) {

        List<LoanPlainDto> loanPlainDtoList = new ArrayList<>();

        for (LoanPlain loanPlain : loanPlainList) {
            LoanPlainDto loanPlainDto = new LoanPlainDto();
            loanPlainDto.setUserId(loanPlain.getUserId());
            //
            loanPlainDtoList.add(loanPlainDto);
        }
        return loanPlainDtoList;
    }
}
